import java.io.*;
import java.net.*;

public class TcpServer {
    public static void main(String[] args) {
        int port = 12345; // 服务器端口号
        try (ServerSocket serverSocket = new ServerSocket(port)) {
            System.out.println("服务器已启动，等待客户端连接...");
            while (true) {
                try (Socket clientSocket = serverSocket.accept();
                     PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true);
                     BufferedReader in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()))) {

                    System.out.println("客户端已连接");

                    String inputLine;
                    while ((inputLine = in.readLine()) != null) {
                        System.out.println("收到客户端消息: " + inputLine);
                        out.println("服务器: " + inputLine);
                    }
                } catch (IOException e) {
                    System.err.println("客户端连接出错: " + e.getMessage());
                }
            }
        } catch (IOException e) {
            System.err.println("无法启动服务器: " + e.getMessage());
        }
    }
}

